import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Confirm from '#cpt/temp/Confirm'
import FormModal from '#cpt/temp/FormModal'
import Controls from '#cpt/temp/Controls'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== $Lazy
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== Data
const Status = [{name: '待审核', value: '0'}, {name: '审核通过', value: '1'},{name: '审核不通过', value: '-1'}]
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		// 搜索表单列表
		forms : [
			{ label: '状态',		name: 'status',					type:'select', 		data:Status },
			{ label: '账期',		name: 'phase_id',				type:'select',		loading:true },
			{ label: '客户名称',		name: 'hosp_id',				type:'select',		loading:true },
			{ label: '项目名称',		name: 'kind_id',				type:'select',		loading:true },
			{ label: '创建时间',		names: ['start_at','end_at'], 	type:'date-range', 	loading:true },
		],
		formData:[
			{ label: '取消原因', 	name:'remark', 		full:true, 	type:'textarea',	required:true },
		],
	}
	submit = {}
	
	componentDidMount(){
		this.getSelect()
		if(window.isInit){
			this.fetch()
		}
	}
	
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		
		$cache.then( async f => {
			// 账期
			$fn.setRow(forms, 'phase_id', await f.getCache({ api:'billPhaseSelect' } ) ) 
			// 客户名称
			$fn.setRow(forms, 'hosp_id', await f.getCache({ api:'hospital' } ) ) 
			// 项目名称
			$fn.setRow( forms, 'kind_id', await f.getCache({ api:'labkindselect' } ) )
			this.setState({ forms })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'bill/billUpPriceAuditList' )
	
	// 表单
	cols = [
		{ title: '账期', 		dataIndex: 'summary_sn', 		width:100 },
		{ title: '医院名称', 	dataIndex: 'hosp_name',  		width:150 },
		{ title: '项目名称', 	dataIndex: 'kind_name', 		width:200 },
		{ title: '状态', 		dataIndex: 'status_name', 		width:100 },
		{ title: '创建人', 		dataIndex: 'create_user', 		...$fn.table.name },
		{ title: '创建时间', 	dataIndex: 'created_at',  		...$fn.table.time},
		{ title: '审核人', 		dataIndex: 'audit_user', 		...$fn.table.name },
		{ title: '审核时间', 	dataIndex: 'audit_at',  		...$fn.table.time},
		{ title: '不通过原因', 	dataIndex: 'remark',  			...$fn.table.time},
		{ title: '标准价',		dataIndex: 'sprice', 			width:100 },
		{ title: '结算价',		dataIndex: 'price', 			width:100 },
	]
	
	// 操作按钮
	controls = () => {
		let keys = []
		this.state.selectedRows.forEach(v=>{
			if(+v.status === 0){
				keys.push(v.uuid)
			}
		})
		return [
			Confirm.call(this,'审核通过','bill/batchUpPriceAudit', { disabled: !$fn.hasArray(keys), query:{uuids:keys} }),
			Controls.click.call(this, '审核不通过', ()=>{
				this.submit.uuids = keys
				this.modal.open({formRef: this.formRef})
			},{disabled: !$fn.hasArray(keys)}),
		]
	}
	
	render(){
		return (
			<>
				<Page title='待结算审核'  controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this, {isInit: window.isInit}) }
				</Page>
				{FormModal.call(this, {title:'取消审核', labelWidth:'s6', width:700, api:'bill/billUpPriceCancel' } )}
			</>
		)
	}
}

export default Index
// 新增待结算审核 2022-4-22  --马婷 